package org.hackystat.devcathlon.ui.page.teams.browse;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.engine.user.User;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.DevcathlonBasePage;
import org.hackystat.devcathlon.ui.page.profile.ProfilePage;
import org.hackystat.devcathlon.ui.page.teams.profile.TeamsProfile;

/**
 * Implements the Teams Browse page, which provides a summary of the Teams associate with 
 * the Devcathlon System.
 * 
 * @author Ka Hung Phillip Lau
 */
public class TeamsBrowsePage extends DevcathlonBasePage {

  /** Support serialization. */
  private static final long serialVersionUID = 1L; 
  
  /**
   * Creates the Teams Browse page.  
   * This page  contain information of all teams in Devcathlon System.  
   */
  public TeamsBrowsePage () {
    // Set the body class attribute to section-2 so that the menubar is correct.
    add(new WebMarkupContainer("body") { 
      private static final long serialVersionUID = 1L;
      @Override
      public boolean isTransparentResolver() { 
        return true; 
        } 
      @Override
      protected void onComponentTag(ComponentTag tag) { 
        tag.put("class","thrColElsHdr section-2"); 
        }
    }
    ); 
    
    // Make the tab corresponding to this page bold.
    get("TeamsBrowseLink").add(new SimpleAttributeModifier("class", "selected"));

    //******************************************************
    //Retriving All Teams
    
    List<Team> teams = getTeams();
    
    // Creates and sets a message if no teams are displayed.
    String status = "";
    if (teams.isEmpty()) {
      status = "Devcathlon have no teams";
    }
    add(new Label("StatusLabel", status));
    
    // Create and populate the table with the teams 
    // the user is associated with.
    ListView listview = new ListView("TeamsList", teams) {      
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(final ListItem item) {
        final Team team = (Team) item.getModelObject();
        
        item.add(new AttributeModifier("class", true, new AbstractReadOnlyModel() {
          private static final long serialVersionUID = 1L;

          public Object getObject() {
              return (item.getIndex() % 2 == 0) ? "odd" : "even";
          }
      }));
        
        try {
          BufferedDynamicImageResource resource = new BufferedDynamicImageResource();     
          BufferedImage image;      
          image = ImageIO.read(new File(team.getTeamLogoPath()));
          resource.setImage(image);
          item.add(new Image("TeamLogo", resource)); 
        }
        catch (IOException e) {
          item.add(new Image("TeamLogo"));
        }
        
        Link teamProfileLink = new Link("TeamProfileLink") {
          private static final long serialVersionUID = 1L;
          @Override
          public void onClick() { 
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team.getName());
            setResponsePage(new TeamsProfile());
          }          
        };
        teamProfileLink.add(new Label("TeamNameLabel", team.getName()));
        
        List<User> memberList = new ArrayList<User> (team.getMembers());
        Collections.sort(memberList);
        ListView memberListview = new ListView("MembersList", memberList) {      
          private static final long serialVersionUID = 1L;

          @Override
          protected void populateItem(ListItem item) {
            final User member = (User) item.getModelObject();
            Link memberLink = new Link ("MemberProfileLink") {
              private static final long serialVersionUID = 1L;

              @Override
              public void onClick() {
                DevcathlonSession.get().getProfileSession().setProfile(member.getEmail());
                setResponsePage(ProfilePage.class);
              }              
            };
            
            memberLink.add(new Label("MemberEmailLabel", 
                member.getFirstName() + " " + member.getLastName()));
            item.add(memberLink);
          }
        };       
        item.add(memberListview);
        
        item.add(teamProfileLink);
        item.add(new Label("TeamProjectLabel", team.getProjectName()));
        
        Link ownerLink = new Link ("OwnerProfileLink") {
          private static final long serialVersionUID = 1L;

          @Override
          public void onClick() {
            DevcathlonSession.get().getProfileSession().setProfile(team.getOwner().getEmail());
            setResponsePage(ProfilePage.class);
          }              
        };
        
        ownerLink.add(new Label("TeamOwnerLabel", 
            team.getOwner().getFirstName() + " " + team.getOwner().getLastName()));
        item.add(ownerLink);
      }
    };       
    add(listview);
    
  }
  
  /** 
   * Get the teams that are in the system.
   * @return The list of teams.
   */
  @SuppressWarnings("unchecked")
  private List<Team> getTeams() {
    List<Team> teams = new ArrayList<Team>();
  
    for (Team team : (List<Team>) TeamManager.getInstance().getAllTeams()) { 
        teams.add(team);
    }
    return teams;
  }
  
}
